package com.seeyon.apps.brwh.dao.impl;

import com.seeyon.apps.brwh.dao.CustomOrgDao;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.OrgConstants;
import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgUnit;
import com.seeyon.ctp.organization.po.OrgMember;
import com.seeyon.ctp.organization.po.OrgUnit;
import com.seeyon.ctp.util.DBAgent;
import org.apache.commons.collections.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class CustomOrgDaoImpl implements CustomOrgDao {

    /**
     * 根据更新时间查找组织单元数据
     *
     * @param updateTime 更新时间
     * @return 组织单元数据列表
     * @throws BusinessException 业务异常
     */
    public List<V3xOrgUnit> findOrgUnitData(Date updateTime) throws BusinessException {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();
        hql.append(" FROM " + OrgUnit.class.getSimpleName());
        hql.append(" WHERE group = false and code != '' and deleted = false ");

        // 如果更新时间不为空，则添加条件筛选
        if (updateTime != null) {
            hql.append(" and CONVERT(VARCHAR(30), updateTime, 23) = :updateTime ");
            params.put("updateTime", new SimpleDateFormat("yyyy-MM-dd").format(updateTime));
        }

        hql.append(" ORDER BY sortId ASC,id");

        // 查询数据库
        List<OrgUnit> list = DBAgent.find(hql.toString(), params);

        // 如果查询结果为空，则返回空列表
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>(0);
        }

        // 将查询结果转换为 V3xOrgUnit 对象列表
        return list.stream()
                .map(po -> {
                    V3xOrgUnit bo;
                    if (((OrgUnit) po).getType().equals(OrgConstants.UnitType.Account.name())) {
                        bo = (V3xOrgUnit) new V3xOrgAccount().fromPO(po);
                    } else {
                        bo = (V3xOrgUnit) new V3xOrgDepartment().fromPO(po);
                    }
                    return bo;
                })
                .collect(Collectors.toList());
    }


    /**
     * 根据更新时间查询组织成员数据
     *
     * @param updateTime 更新时间
     * @return 组织成员数据列表
     * @throws BusinessException 业务异常
     */
    @Override
    public List<OrgMember> findOrgMemberData(Date updateTime) throws BusinessException {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();
        hql.append(" FROM " + OrgMember.class.getSimpleName());
        hql.append(" WHERE admin = false and internal = true and code != '' and deleted = false ");
        if (updateTime != null) {
            hql.append(" and CONVERT(VARCHAR(30), updateTime, 23) = :updateTime ");
            params.put("updateTime", new SimpleDateFormat("yyyy-MM-dd").format(updateTime));
        }
        hql.append(" ORDER BY sortId ASC,id");
        return DBAgent.find(hql.toString(), params);
    }

}
